package DiTie;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DengLu {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入工号");
        String s1 = sc.next();
        System.out.println("请输入姓名");
        String s2 = sc.next();
        ArrayList<Manager> list1 = new ArrayList<>();
        ArrayList<Subway> list2 = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("jinjie\\src\\DiTie\\managers.txt"));
        String len;
        while ((len = br.readLine()) != null) {
//            System.out.println(len);
            String num = len.split(",")[0];
//            System.out.println(len);
            String name = len.split(",")[1];
            String xb = len.split(",")[2];
            String phone = len.split(",")[3];
            Manager m1 = new Manager(num, name, xb, phone);
            list1.add(m1);
        }
        br.close();
        BufferedReader br2 = new BufferedReader(new FileReader("jinjie\\src\\DiTie\\managers.txt"));
        String len2;
        while ((len2 = br2.readLine()) != null) {
            String num = len2.split(",")[0];
            String name = len2.split(",")[1];
            String lx = len2.split(",")[2];
            Subway ss = new Subway(num, name, lx);
            list2.add(ss);
        }
        br2.close();
        boolean flag = false;
        for (Manager m : list1) {
            String num = m.getNum();
            String name = m.getName();
            if (s1.equals(num) && s2.equals(name)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("登陆成功");
            System.out.println("1.查看地铁站 2.添加地铁站 3.退出");
            String num = sc.next();
            switch (num) {
                case "1":
                    findsubway(list2);
                    break;
                case "2":
                    addSubway(list2);
                    break;
                default:
                    break;
            }
        } else {
            System.out.println("登陆失败");
        }
    }


    private static void addSubway(ArrayList<Subway> list2) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入地铁站编号:");
        String sid = sc.nextLine();
        System.out.println("请输入地铁站名称:");
        String sname = sc.nextLine();
        System.out.println("请输入所属路线:");
        String line = sc.nextLine();
        Subway subway = new Subway(sid, sname, line);
        list2.add(subway);
        BufferedWriter bw = new BufferedWriter(new FileWriter("jinjie\\src\\DiTie\\subways.txt"));
        for (Subway sub : list2) {
            String s = sub.getNum() + "," + sub.getName() + "," + sub.getLx();
            bw.write(s);
            bw.newLine();
        }
        bw.close();
    }

    private static void findsubway(ArrayList<Subway> list2) {
        if (list2 == null) {
            System.out.println("没有地铁站信息!!!");
        } else {
            for (Subway subway : list2) {
                System.out.println(subway);
            }
        }
    }
}

